//
//  StepsTableViewController.m
//  TutorialsModule
//
//  Created by Augusto Guido on 30/09/09.
//  Copyright 2009 Moove-IT. All rights reserved.
//

#import "StepsTableViewController.h"
#import "StepViewController2.h"
#import "NewStepViewController2.h"
#import "TutorialViewController2.h"


@implementation StepsTableViewController

@synthesize steps;
@synthesize new;

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
		steps = [[NSArray alloc] initWithObjects:@"Step1", @"step 2",nil];
    }
    return self;
}
*/

-(id) initWithNibName: (NSString*) nibNameOrNil bundle:(NSBundle *)nibBundleOrNil objects: (NSArray *)objects{
	
	if(self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
	{
		self.title = @"Steps";
		steps = [[NSMutableArray alloc] initWithObjects:@"Step 1", @"Step 2", @"Step 3", @"Step 4", @"Step 5", nil];
	}
	return self;
	
}


- (void)viewDidLoad {
    [super viewDidLoad];
	//Aca se tendría que preguntar si es nuevo o no.
	if (self.new){
		UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(finish)];
		self.navigationItem.leftBarButtonItem = back;
		[back release];
	}
	self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)finish {
	TutorialViewController2 *tutorialViewController = [[TutorialViewController2 alloc] initWithNibName:@"TutorialViewController2" bundle:nil];
	[self.navigationController popToRootViewControllerAnimated:YES];
	[tutorialViewController release];
	
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (section == 0){
		return [steps count];
	}else
		return 1;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    if (indexPath.section == 0){

		UIImageView *photo;		
        photo = [[[UIImageView alloc] initWithFrame:CGRectMake(240.0, 2.0, 50.0, 40.0)] autorelease];		
		photo.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"edit_foto.jpg" ofType:nil]];		
        photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;		
        [cell.contentView addSubview:photo];
		
		UILabel *textLabel;
		textLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 5, 145, 20)] autorelease];
		textLabel.text = [steps objectAtIndex:indexPath.row];
		textLabel.font = [UIFont systemFontOfSize:14];
		[cell.contentView addSubview:textLabel];
		
	}else{
		cell.textLabel.text = @"Add step";
		cell.textLabel.textAlignment = UITextAlignmentCenter;
		
	}
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 1){
		NewStepViewController2 *newStep = [[NewStepViewController2 alloc] initWithNibName:@"NewStepViewController2" bundle:nil];
		
		[self.navigationController pushViewController:newStep animated:YES];
		[newStep release];
	}else{
		if (indexPath.section == 0){
			StepViewController2 *stepViewController = [[StepViewController2 alloc] initWithNibName:@"StepViewController2" bundle:nil];
			stepViewController.title = [steps objectAtIndex:indexPath.row];
			[self.navigationController pushViewController:stepViewController animated:YES];
			[stepViewController release];
		}
	}
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0)
		return YES;
	else return NO;
}


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
	if (fromIndexPath.section == toIndexPath.section) {
		NSObject * tmp = nil;
		NSObject * tmp2 = nil;
		tmp = [steps objectAtIndex:fromIndexPath.row];
		if (fromIndexPath.row>toIndexPath.row) {
			for (int i = fromIndexPath.row; i>toIndexPath.row;i--)
			{
				tmp2 = [steps objectAtIndex:i-1];
				[steps replaceObjectAtIndex:i withObject:tmp2];
			}
			[steps replaceObjectAtIndex:toIndexPath.row withObject:tmp];
		}
		else if (fromIndexPath.row<toIndexPath.row) {
			for (int i = fromIndexPath.row; i<toIndexPath.row;i++)
			{
				tmp2 = [steps objectAtIndex:i+1];
				[steps replaceObjectAtIndex:i withObject:tmp2];
			}
			[steps replaceObjectAtIndex:toIndexPath.row withObject:tmp];
		}
	}
}


// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0)
		return YES;
	else return NO;
}

- (void)dealloc {
    [super dealloc];
	[steps release];
}


@end

